package com.hbpa.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.hbpa.dao.StudentJdDao;
import com.hbpa.form.StudentJdForm;
import com.hbpa.util.ConnectionFactory;
import com.hbpa.util.JdbcUtil;

public class StudentJdDaoImpl implements StudentJdDao {

	public boolean addStudentJd(String xh, String zwjd) {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = ConnectionFactory.getConnection();
			con.setAutoCommit(false);
			ps = con.prepareStatement("insert into st_zwjd(xh,jd) values(?,?)");
			ps.setString(1, xh);
			ps.setString(2, zwjd);  
			ps.executeUpdate();
			con.commit();
			return true;
		} catch (SQLException e) {
			if(con!=null){try{con.rollback();}catch(Exception ex){}}
			e.printStackTrace();
		}finally{
			JdbcUtil.close(ps, con);
		}
		return false;
	}

	public boolean checkStudentJd(String xh) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = ConnectionFactory.getConnection(); 
			ps = con.prepareStatement("select count(*) as c from st_zwjd where xh = ?"); 
			ps.setString(1,xh); 
			 
			rs = ps.executeQuery();
			if(rs.next())
			{ 
				String count = rs.getString("c");
				if(count.trim().equals("1")){
					return true;
				}else{
					return false;
				}
			}
			 
		} catch (SQLException e) { 
			e.printStackTrace();
		}finally{
			JdbcUtil.close(rs,ps,con);
		}
		return false;
	}

	public boolean updateStudentJd(String xh, String zwjd) {
		
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = ConnectionFactory.getConnection();
			con.setAutoCommit(false);
			ps = con.prepareStatement("update st_zwjd set jd=? where xh=?");
			ps.setString(1, zwjd);
			ps.setString(2, xh);  
			ps.executeUpdate();
			con.commit();
			return true;
		} catch (SQLException e) {
			if(con!=null){try{con.rollback();}catch(Exception ex){}}
			e.printStackTrace();
		}finally{
			JdbcUtil.close(ps, con);
		}
		return false;
	}

	public StudentJdForm getStudentJdByXh(String xh) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		StudentJdForm studentJdForm = null;
		try {
			studentJdForm = new StudentJdForm();
			con = ConnectionFactory.getConnection(); 
			ps = con.prepareStatement("select * from st_zwjd where xh = ?"); 
			ps.setString(1,xh); 
			 
			rs = ps.executeQuery();
			if(rs.next())
			{ 
				String zwjd = rs.getString("jd");
				studentJdForm.setZwjd(zwjd); 
				studentJdForm.setId(rs.getString("id"));
				studentJdForm.setXh(rs.getString("xh"));
			}
			 return studentJdForm;
		} catch (SQLException e) { 
			e.printStackTrace();
		}finally{
			JdbcUtil.close(rs,ps,con);
		}
		return null;
	}

	
}
